package com.jz.community.basecomm.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.bean.baseCoupon.BaseCouponInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/5/29 15:16
 * 新用户注册,绑定手机号发放 优惠券
 */
public class GetCouponModeTask extends RxTask<String, Integer, BaseResponseInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetCouponModeTask(Activity activity,
                             ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected BaseResponseInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Constant.RECEIVE_COUPON_MODE_URL, getParam(params[0]),true);
        BaseResponseInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, BaseResponseInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(BaseResponseInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    private HashMap<String, Object> getParam(String receiveType) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("app-code", AppConstants.APPID);
        map.put("receive-type", ConverterUtils.toInt(receiveType));
        return map;
    }
}